<?php
class Core_Auth {
	protected $_objAuth = null;
		
	public function __construct($namespace = 'default'){
		$this->_objAuth = Zend_Auth::getInstance();
		$sessionStorage = new Zend_Auth_Storage_Session($namespace);
		$this->_objAuth->setStorage($sessionStorage);		
	}
	
	public function login($username, $password){
		if (trim($username) == '' || trim($password) == '')
			return false;		
		$authAdapter=new Zend_Auth_Adapter_DbTable(
				Zend_Registry::get('db'),
				'users',
				'username',
				'password',
				'MD5(?)'
		);
		$authAdapter->setIdentity($username);
		$authAdapter->setCredential($password);
		$result=$this->_objAuth->authenticate($authAdapter);
	
		if($result->isValid()){
			$storage=$this->_objAuth->getStorage();
			$arrUser=$authAdapter->getResultRowObject();			
			$storage->write($arrUser);
			return true;
		}
		return $result;
	}
	
	public function getIdentity() {		
		if ($this->_objAuth->hasIdentity()) {
			$user = $this->_objAuth->getIdentity();
			$user->userIp = $_SERVER['REMOTE_ADDR'];
			$user->sessionId = session_id();		
			return $user;
		}
		return false;
	}
	
	public function isLogged() {
		return $this->_objAuth->hasIdentity();
	}
	
	public function logout() {
		$this->_objAuth->clearIdentity();
	}
}